<?php namespace Core\Myapp\View;

class View extends \Core\Myapp\Zobero
{
    protected $_area = 'frontend';
    protected $_themePackage = 'default';
    protected $_layout = 'two-columns-left';
    protected $_regions = array('header', 'footer', 'left-col', 'right-col', 'content');
    protected $_parts = array(
        'head'=>'page/parts/head.phtml',
        'header'=>'page/parts/header.phtml',
        'toolbar'=>'page/parts/toolbar.phtml',
        'navigation'=>'page/parts/navigation.phtml'
    );
    protected $_js = array();
    protected $_css = array();
    // Meta Data
    protected $_title;
    protected $_keywords;
    protected $_description;
    protected $_contentType = 'text/html; charset=UTF-8';

    public function setThemePackage()
    {
        // load config to get active theme package
        $this->_themePackage = 'default';
    }
    public function getThemePackage()
    {
        return $this->_themePackage;
    }
    public function getParts()
    {
        return $this->_parts;
    }
    public function setLayout($name)
    {
        $this->_layout = $name;
        return $this;
    }
    public function addRegion($name)
    {
        $this->_regions[] = $name;
        return $this;
    }
    public function setArea($area)
    {
        $this->_area = $area;
        return $this;
    }
    public function getArea()
    {
        return $this->_area;
    }
    public function renderRegion($name)
    {

    }
    public function render()
    {
        $layoutPath = 'page' . DS . $this->_layout . '.phtml';
        $html = $this->fetchTemplate($layoutPath);
        $this->getResponse()->setBody($html)->sendResponse();
    }
    protected function getResponse()
    {
        return $this->getInstance('\Core\Zend\Controller\Response\Http');
    }
    public function loadPart($name)
    {
        $parts = $this->getParts();
        $path = $parts[$name];
        return $this->fetchTemplate($path);
    }
    protected function fetchTemplate($path)
    {
        $filePath = FRONTEND_DESIGN_PATH . DS . $this->getThemePackage() . DS . 'template' . $path;
        ob_start();
        try {
            include $filePath;
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
        $html = ob_get_clean();
        return $html;
    }
    public function setTitle($title)
    {
        $this->_title = $title;
    }
    public function setKeywords($keywords)
    {
        $this->_keywords = htmlspecialchars($keywords);
    }
    public function setDescription($description)
    {
        $this->_description = htmlspecialchars($description);
    }
    public function setContentType($contentType)
    {
        $this->_contentType = htmlspecialchars($contentType);
    }
    public function getTitle()
    {
        return $this->_title;
    }
    public function getKeywords()
    {
        return $this->_keywords;
    }
    public function getDescription()
    {
        return $this->_description;
    }
    public function getContentType()
    {
        return $this->_contentType;
    }
    public function addJs($path)
    {
        $url = $this->getSingleton('\Core\Myapp\Helper\Url');
        $this->_js[] = $url->getJsUrl($path);
        return $this;
    }
    public function loadJs()
    {
        $js = '';
        foreach($this->_js as $key=>$path) {
            $js .= '<script src="' . $path . '"></script>\n';
        }
        return $js;
    }
    public function loadCss()
    {

    }
    public function getCssUrl($path)
    {
        $url = $this->getSingleton('\Core\Myapp\Helper\Url');
        return $url->getCssUrl($path);
    }
    public function getJsUrl($path)
    {
        $url = $this->getSingleton('\Core\Myapp\Helper\Url');
        return $url->getJsUrl($path);
    }
}